package b3.widget.match
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.scene.build.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.tab.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class MatchResultDialog extends TabController
	{
		private var match:Match;
		
		public function MatchResultDialog(match:Match)
		{
			this.match = match;
			var matchConfig:MatchConfig = Config.matchConfigs[match.index];
			var MatchDialogBgr:Class = ResourceHelper.getInstance().getClassByName("MatchDialogBgr");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("MatchTitileBgr");
			var HighlightTitleBgr:Class = ResourceHelper.getInstance().getClassByName("MatchTitileBgr2");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton1");
			var TabPageFirstPage:Class = ResourceHelper.getInstance().getClassByName("TabPageFirstPage3");
			var TabPagePageUp:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp3");
			var TabPagePageDown:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown3");
			var TabPageLastPage:Class = ResourceHelper.getInstance().getClassByName("TabPageLastPage3");
			super(MatchDialogBgr, CloseButton);
			this.anchor = BasicWidget.ANCHOR_MIDDLE_MIDDLE;
			var tab:MatchResultTab = new MatchResultTab(0);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(250, 268);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new MatchResultTab(1);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(250, 268);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new MatchResultTab(2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(250, 268);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new MatchResultTab(3);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(250, 268);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			this.setTabTitlePos(57, -22, 79); 
			this.setTabPagePos(37, 94);
			this.setCloseButtonPos(231, 392);
			var buildingConfig:BuildingConfig = Config.buildingConfigs[matchConfig.buildingIndex];
			var BuildingImage:Class = ResourceHelper.getInstance().getClassByName(buildingConfig.iconImage[matchConfig.roofType]);   
			var buildingImg:Sprite = new BuildingImage();
			buildingImg.alpha = 0.8;
			buildingImg.x = 66;
			buildingImg.y = 404 - buildingImg.height;
			addChild(buildingImg);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
			match.addEventListener(ModelEvent.REFRESH_MATCH_RANK_LIST, onRefreshMatchRankList);
			match.loadMatchRankList(true);
		}

		private function onRemovedFromStage(event:Event):void
		{
			match.removeEventListener(ModelEvent.REFRESH_MATCH_RANK_LIST, onRefreshMatchRankList);
		}
		
		private function onRefreshMatchRankList(event:Event):void
		{
			removeAllItems();
			for (var i:int = 0; i < match.ranks.length; i++)
			{
				var matchTab:MatchResultTab = this.tabs[i];
				var myRank:int = -1;
				for (var j:int = 0; j < match.ranks[i].length; j++)
				{
					var rankInfo:RankInfo = match.ranks[i][j];
					var matchItem:MatchItem = new MatchItem(rankInfo);
					if (rankInfo.userId == PlayerManager.getInstance().me.userId && (j < myRank || myRank == -1))
						myRank = j;
					matchTab.addItem(matchItem, matchItem.width, matchItem.height);
				}
				matchTab.setMyRanking(myRank);		 
			} 
		}
		
	}
}